package cn.zh.npro.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import cn.zh.npro.Item.HFItem;
import cn.zh.npro.R;

/**
 * Created by WYN on 2018/2/27 0027.
 */

public class HFAdapter extends BaseAdapter {
    Context context;
    ArrayList<HFItem> arrayList;
    LayoutInflater inflater;
    int type=0;
    public HFAdapter(Context context,ArrayList<HFItem> arrayList,int type){
        this.context=context;
        this.arrayList=arrayList;
        inflater=LayoutInflater.from(context);
        this.type=type;
        }

    @Override
    public int getCount() {
        return arrayList.size();
        }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder=null;
        if (null==view){
            holder=new Holder();
            view=inflater.inflate(R.layout.hfl_layout,null,false);
            holder.hfdata=view.findViewById(R.id.hfdata);
            holder.hforder=view.findViewById(R.id.hforder);
            holder.hfscale=view.findViewById(R.id.hfscale);
            holder.imagd=view.findViewById(R.id.imagd);
            holder.imagup=view.findViewById(R.id.imagup);
            view.setTag(holder);
           }else {
            holder= (Holder) view.getTag();
            }
           if (0!=arrayList.size()){
               holder.hfdata.setText(arrayList.get(i).getSndTime());
               if (0==type){
                   holder.hforder.setText("￥"+arrayList.get(i).getGoodsTotal());
               }else {
                   holder.hforder.setText(arrayList.get(i).getGoodsTotal()+"");
                  }
               if (arrayList.get(i).getGoodsTotal1()==0){
                   holder.imagd.setVisibility(View.GONE);
                   holder.imagup.setVisibility(View.GONE);
                   holder.hfscale.setText("--");
               }else {
                   double rate=(arrayList.get(i).getGoodsTotal()-arrayList.get(i).getGoodsTotal1())/arrayList.get(i).getGoodsTotal1();
                   double ate=0;
                   if (rate>0){
                       holder.imagd.setVisibility(View.GONE);
                       holder.imagup.setVisibility(View.VISIBLE);
                       ate=rate;
                   }else {
                       holder.imagd.setVisibility(View.VISIBLE);
                       holder.imagup.setVisibility(View.GONE);
                       ate=-rate;
                   }
                   DecimalFormat df=new DecimalFormat("#0.00");
                   String fate=String.valueOf(df.format(ate*100));
                   holder.hfscale.setText(fate+"%");
               }
              }
           return view;
           }

   public class Holder{
        TextView hfdata,hforder,hfscale;
       ImageView imagd,imagup;
      }


}
